<?php
defined('_JEXEC') or die('Restricted access');
class counterHelper
{
	 function getCounter($params)
	{

		// $weburl=JURI::base();
		// $db		=& JFactory::getDBO();	
		$s_today=$params['s_today'];
		$s_yesterday=$params['s_yesterday'];
		$s_all=$params['s_all'];
		$s_week=$params['s_week'];
	 	$s_digit=$params['s_digit'];
		$s_month=$params['s_month'];
		$disp_type=$params['disp_type'];
		
		$initialvalue    =    1;
		$records        =    100000;
		$widthtable        =    '80';	
		$pretext        =     '';
		$posttext        =     '';
		$locktime =15;
		$locktime        =    $locktime * 60;
		
		$day             =    date('d');
		$month             =    date('n');
		$year             =    date('Y');
		$daystart         =    mktime(0,0,0,$month,$day,$year);
		$monthstart         =  mktime(0,0,0,$month,1,$year);

		$weekday         =    date('w');
		$weekday--;
		if ($weekday < 0)    $weekday = 7;
		$weekday         =    $weekday * 24*60*60;
		$weekstart         =    $daystart - $weekday;

		$yesterdaystart     =    $daystart - (24*60*60);
		$now             =    time();
		$ip                 =    $_SERVER['REMOTE_ADDR'];
		
		$sql             =    "SELECT MAX(id) AS total FROM dm_counteronlineuser";	
		
		$rs=mysql_query($sql);
		$t=mysql_fetch_assoc($rs);
		
		// $query = $db->setQuery( $sql );	
		// $t = $db->loadAssoc();		
		$all_visitors     =    $t['total'];
			  	// echo "<pre>";
// print_r($all_visitors);
// echo "</pre>";
		if ($all_visitors !== NULL) {
			$all_visitors += $initialvalue;
		} else {
			$all_visitors = $initialvalue;
		}

		$temp = $all_visitors - $records;
		
		if ($temp>0){		
			$sql         =  "DELETE FROM dm_counteronlineuser WHERE id<'$temp'";
			// $db->setQuery( $sql );		
			// $db->query();	
			mysql_query($sql);	   		
		}
		
		$sql             =    "SELECT COUNT(id) AS visitip FROM dm_counteronlineuser WHERE ip='$ip' AND (tm+'$locktime')>'$now'";		
		// $query = $db->setQuery( $sql );	
		// $vip = $db->loadAssoc();	

		$rs=mysql_query($sql);
		$vip=mysql_fetch_assoc($rs);		

		$items             =    $vip['visitip'];
	
		if (empty($items))
		{
			$sql = "INSERT INTO dm_counteronlineuser (tm, ip) VALUES ('$now', '$ip')";
			// $db->setQuery( $sql );		
			// $db->query();	
			mysql_query($sql);				
		}
		
		$n                 =     $all_visitors;
	//	$div = 100000000;
		$div = 100000;
		while ($n > $div) {
			$div *= 10;
		}		
		
		if($s_today)
		{
			$sql             =    "SELECT COUNT(id) AS todayrecord FROM dm_counteronlineuser WHERE tm>'$daystart'";		
			// $query = $db->setQuery( $sql );	
			// $todayrc = $db->loadAssoc();	
			
			$rs=mysql_query($sql);
			$todayrc=mysql_fetch_assoc($rs);	
		
			$today_visitors     =    $todayrc['todayrecord'];
		}
		
		if($s_yesterday)
		{
			$sql             =    "SELECT COUNT(id) AS yesterdayrec FROM dm_counteronlineuser WHERE tm>'$yesterdaystart' and tm<'$daystart'";
			// $query = $db->setQuery( $sql );	
			// $yesrec = $db->loadAssoc();	
			
			$rs=mysql_query($sql);
			$yesrec=mysql_fetch_assoc($rs);
			
			$yesterday_visitors     =    $yesrec['yesterdayrec'];
		}
		
		if($s_week)
		{
			$sql             =    "SELECT COUNT(id) AS weekrec FROM dm_counteronlineuser WHERE tm>='$weekstart'";		
			// $query = $db->setQuery( $sql );	
			// $weekrec = $db->loadAssoc();	
			
			$rs=mysql_query($sql);
			$weekrec=mysql_fetch_assoc($rs);
			$week_visitors     =    $weekrec['weekrec'];
		}

		if($s_month)
		{
			$sql             =    "SELECT COUNT(id) AS monthrec FROM dm_counteronlineuser WHERE tm>='$monthstart'";		
			// $query = $db->setQuery( $sql );	
			// $monthrec = $db->loadAssoc();	
			$rs=mysql_query($sql);
			$monthrec=mysql_fetch_assoc($rs);
			$month_visitors     =    $monthrec['monthrec'];
		}
		
		$counter = '<div class="counter">';
		if ($pretext != "") $counter .= '<div>'.$pretext.'</div>';
		
		// Show digit counter
		if($s_digit){      

			$counter .= '<div style="text-align: center; padding-top: 5px;">';
			while ($div >= 1) {			
				$digit = $n / $div % 10;
				$imglink =BASE_URL.'/mods/counter/images/visiter/'.$disp_type.'/'.$digit.'.png';
				$counter .= "<img src='$imglink' alt='Vister' title='Visters' />";
				$n -= $digit * $div;
				$div /= 10;
			}
			$counter .= '</div>';
		}
		else
		{
			$counter .= '<div style="text-align: center;">';
			while ($div >= 1) {
				$digit = $n / $div % 10;
				$tongonline= $digit;
				$n -= $digit * $div;
				$div /= 10;
			}
			$counter .= '</div>';
			
		}
		
		
		$counter         .=    '<div style="margin:0 auto;margin-left:5px; text-align:center"><table cellpadding="1" cellspacing="1" border="0" style="text-align: center; width: '.$widthtable.'%;"><tbody align="center">';
	
	// Show today, yestoday, week, month, all statistic
    if($s_today)        $counter        .=    counterHelper::spaceer("vtoday.png", "Hôm nay", $today_visitors);
    if($s_yesterday)    $counter        .=    counterHelper::spaceer("vyesterday.png", "Hôm qua", $yesterday_visitors);
    if($s_week)         $counter     .=    counterHelper::spaceer("vweek.png", "Tuần này", $week_visitors);
    if($s_month)        $counter        .=    counterHelper::spaceer("vmonth.png", "Tháng này", $month_visitors);
    if($s_all)          $counter      .=    counterHelper::spaceer("vall.png", "Tất cả", $all_visitors);
		
		$counter        .= "</tbody></table></div>";
		$counter .= "</div>";
		if ($posttext != "") $counter        .= '<div>'.$posttext.'</div>';

$hienthi = "<div class='footer-thongke'>";
$hienthi .="<div><div class='thongke-div'>Hôm nay:</div><div class='thongke-div2'>$today_visitors</div></div><br>";
$hienthi .="<div><div class='thongke-div'>Hôm qua:</div><div class='thongke-div2'>$yesterday_visitors</div></div><br>";
$hienthi .="<div><div class='thongke-div'>Tất cả:</div><div class='thongke-div2'>$all_visitors</div></div><br>";
$hienthi .="</div>";

	//	return 	$hienthi;
		return 	$counter;
	}
	
	function spaceer($a1,$a2,$a3)
	{
		$weburl=BASE_URL.'/mods/counter/images/stats/default/';

		$ret = "<tr style=\"text-align:left;\">
				<td width=\"10\"><img src=\"".$weburl.$a1."\" alt=\"Visitor\"/></td>";
		$ret .= "<td width=\"55\">".$a2."</td>";
		$ret .="<td width=\"10\" style=\"text-align:right;\">".$a3."</td></tr>";
		return $ret;
	}  

}